/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.example.omsdkdemo.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.device.idea.om.controller.model.AppUsageStats;
import com.device.idea.om.controller.model.StatPeriod;
import com.example.omsdkdemo.R;
import com.example.omsdkdemo.adapter.AppUsageAdapter;
import com.example.omsdkdemo.manager.OmSdkManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述
 *
 * @since 2023-02-02
 */
public final class AppUsageDialogFragment extends BaseDialogFragment {
    private static final String TAG = "AppUsageDialog";

    private Button getSignBt;

    private TextView signInfoTv;

    private RadioGroup radioGroup;

    private Button appUsageQuery;

    private RecyclerView appUsageRecycleView;

    private AppUsageAdapter appUsageAdapter;

    public AppUsageDialogFragment(OmSdkManager omSdkManager) {
        super(R.layout.app_usage_dialog, omSdkManager.getContext());
        this.omSdkManager = omSdkManager;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        initView();
        initListener();
        builder.setTitle("App使用统计").setView(mView).setPositiveButton("OK", (dialog, which) -> {
        });
        return builder.create();
    }

    /**
     * initView
     */
    @Override
    protected void initView() {
        super.initView();
        getSignBt = mView.findViewById(R.id.bt_app_get_sign);
        signInfoTv = mView.findViewById(R.id.tv_app_sign_info);
        radioGroup = mView.findViewById(R.id.app_usage_radio_group);
        appUsageQuery = mView.findViewById(R.id.bt_app_usage_query);
        appUsageRecycleView = mView.findViewById(R.id.rv_app_usage);
        appUsageAdapter = new AppUsageAdapter(new ArrayList<>());
        appUsageRecycleView.setAdapter(appUsageAdapter);
        appUsageRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
    }

    private void initListener() {
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> queryAppUsage());
        appUsageQuery.setOnClickListener(v -> queryAppUsage());
        getSignBt.setOnClickListener(v -> {
            String signInfo = omSdkManager.getSignature();
            Log.e(TAG, "getSignature signInfo=" + signInfo);
            signInfoTv.setText(signInfo);
        });
    }

    private void queryAppUsage() {
        int checkedId = radioGroup.getCheckedRadioButtonId();
        StatPeriod statPeriod = StatPeriod.ONE_DAY;
        if (checkedId == R.id.three_day) {
            statPeriod = StatPeriod.THREE_DAYS;
        } else if (checkedId == R.id.last_week) {
            statPeriod = StatPeriod.LATEST_MONTH;
        } else if (checkedId == R.id.last_month) {
            statPeriod = StatPeriod.LATEST_MONTH;
        }
        List<AppUsageStats> result = omSdkManager.getAppUsageStats(statPeriod);
        if (result == null) {
            Log.e(TAG, "result is null");
            return;
        }
        appUsageAdapter.updateList(result);
    }
}
